<template>
	<view class="container">
		<!-- s收货地址 -->
		<view class="address">
			<view class="address_info">
				<label for="">收件人:{{receiveInfo.username}}</label>	
				<label for="">{{receiveInfo.userphone}}</label>
			</view>
			<view class="address_res">
				收货地址：<label for="">{{receiveInfo.address}}27号</label>
			</view>
		</view>
		
		<!-- 商品信息 -->
		<view class="carts">
	
			<view class="cartsInfo" v-for="(item,index) in list" :key="item.id">
				<!-- 70px -->
				<view class="carts_detail">`
					<view class="carts_image">
						<image :src="baseUrl+item.img"></image>
					</view>
					<view class="carts_name">
						<view for="" style="font-size: 30rpx;">{{item.goodsname}}</view>
						<view for="" style="font-size: 26rpx; color: #C0C0C0;">规格:300</view>
					</view>
					<view class="carts_price">
						<label for="">￥ {{item.price}}</label>
					</view>
				</view>
				<!-- 50px -->
				<view class="cart_num">
					<view class="">
						购买数量：
					</view>
					<view class="">
						<label @click="reduce(index)" for="" class="jian">-</label>
						<label for="" class="num">{{item.num}}</label>
						<label @click="add(index)" for="" class="jia">+</label>
					</view>
				</view>
				<!-- 横线 -->
				<view class="xian"></view>
			</view>		
				
				
				
			<!-- 50px -->
			<view class="kuaidi">
				<label for="">配送方式</label>
				<label for="">XX快递</label>
			</view>
		</view>
		
		<!-- 优惠券 -->
		<view class="yhquan">
			<label for="">优惠券</label>
			<label for="" style="margin-right: 40rpx;">无可用</label>
		</view>
		<!-- jifen积分 -->
		<view class="jifen">
			<view class="use" >
				<label for="">使用积分</label>
			</view>
			<view class="input" style="margin-right: 40rpx;">
				<input type="text" value="" placeholder="输入积分"/>
				<label for="" class="input_use">使用</label>
				<label for="" class="input_keyi">可使用50积分</label>
			</view>
		</view>
		
		<!-- 具体详细 -->
		<view class="resDetail">
			<view class="">
				<label for="">商品金额</label>
				<label for="" style="margin-right:40rpx;color:red">￥{{total}}</label>
			</view>
			<view class="">
				<label for="">运费</label>
				<label for="" style="margin-right:40rpx;color:red">+￥0.00</label>
			</view>
			<view class="">
				<label for="">优惠券</label>
				<label for="" style="margin-right:40rpx;color:red">-￥0.00</label>
			</view>
			<view class="">
				<label for="">会员优惠</label>
				<label for="" style="margin-right:40rpx;color:red">-￥0.00</label>
			</view>
			<view class="">
				<label for="">积分抵扣</label>
				<label for="" style="margin-right:40rpx;color:red">-￥0.00</label>
			</view>
		</view>
		<!-- 实付金额 -->
		<view class="sfje">
			<!-- {{ allPrice }} -->
			实付金额：<label for="" style="color:red">￥{{total}}</label>
		</view>
		
		<!-- 确认订单按钮 -->
		<view class="btn">
			<!-- 点击提交订单按钮，发起支付请求 -->
			<button type="primary" @click="pay">提交订单</button>
		</view>
	</view>
</template>

<script>
	import {baseUrl} from "../../utils/config.js";
	export default{
		data(){
			return{
				list:[] ,//商品列表
				baseUrl,
				receiveInfo:{
					username:"王俊凯",
					userphone:"18301673847",
					address:"重庆小螃蟹",
				}
			}
		},
		computed:{
			total:function(){
				var result = 0
				this.list.forEach(item=>{
					var price = item.price*item.num
					result += price
				})
				return result;
			}
		},
		onShow(){
			var list = uni.getStorageSync("moneyList")||[];
			this.list = list
			// console.log(this.list);
		},
		methods:{
			//点解添加商品数量
			async add(index){
				this.list[index].num+=1;
				this.cartEdit(index);
			},
			//点解减少商品数量
			reduce(index){
				if(this.list[index].num<=1){
					this.list[index].num=1
				}
				this.list[index].num-=1
				this.cartEdit(index);
			},
			//封装在一块改变数据库num值
			async cartEdit(index){
				var id = this.list[index].id
				var num = this.list[index].num
				var checked = 1;
				var {token} = uni.getStorageSync("userInfo")
				var result=await this.http("/api/cartedit",{id,num,checked},token).catch(err=>{
					console.log(err);
				})
				console.log(result);
			},
			
			async pay(){
				let {uid,token} = uni.getStorageSync("userInfo");
				let {username,userphone,address} = this.receiveInfo;
				var countmoney = this.total;
				var countnumber = 0;
				this.list.forEach(item=>{
					countnumber += item.num
				})
				var addtime = new Date().getTime();
				
				var params = {uid,username,userphone,address,countmoney,countnumber,addtime}
				params = JSON.stringify(params)
				
				//购物车id字符串
				var arr = this.list.map(item=>{
					return item.id;
				})
				console.log(arr);
				var idstr = arr.join(",")
				
				//提交订单
				var result = await this.http("/api/orderadd",{params,idstr},token).catch(err=>{
					console.log(err);
				})
				// console.log(result);
				
				uni.reLaunch({
					url:"/pages/mine/mine"
				})
			}
		},
		
	}
</script>
<style>
	@import url("../../common/css/confirm.css");
</style>
